package com.gxa.eshops.portal.controller;

import com.gxa.eshps.cores.common.JsonResult;
import com.gxa.eshps.cores.common.SnowflakeIdWorker;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.util.Map;
/**
 * @author Administrator
 */
@Api(value = "UploadController", tags = "上传图片接口")
@RestController
@RequestMapping("/img")
public class UploadController {
    @PutMapping("/upImg")
    @ApiOperation("cos图片上传接口")
    public Map<String, Object> cos(File img) {
        JsonResult jsonResult = new JsonResult();
        //文件类型,大小判断
        if (img.length()==0) {
            jsonResult.setCode("0");
            jsonResult.setMsg("不能上传空文件");
            return jsonResult.getMap();
        } else if (img.length() > 500*1024) {
            jsonResult.setCode("0");
            jsonResult.setMsg("上传文件不能超过500kb");
            return jsonResult.getMap();
        }
        String imgName = img.getName();
        String suffix = imgName.substring(imgName.lastIndexOf(".") + 1);
        if ("jpg".equals(suffix) || "jpeg".equals(suffix) || "png".equals(suffix)) {
            // 1 初始化用户身份信息（secretId, secretKey）
            String secretId = "AKID7B90vgOtIWWKI9nEQO3UhEYhdLtELDuR";
            String secretKey = "QddT5bu1yqXBj5EBvS9dtvjIdlfLmkV2";
            COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
            // 2 设置 bucket 的区域
            // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
            Region region = new Region("ap-chengdu");
            ClientConfig clientConfig = new ClientConfig(region);
            //从5.6.54开始,默认使用https
            clientConfig.setHttpProtocol(HttpProtocol.https);
            // 3 生成 cos 客户端。
            COSClient cosClient = new COSClient(cred, clientConfig);
            // 指定要上传到的存储桶
            String bucketName = "wangpeng-1312542869";
            //雪花算法生成名称
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0,0);
            long id = snowflakeIdWorker.nextId();
            // 指定要上传到 COS 上传对象键
            String key = "headImg/"+id+"."+suffix;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, img);
            cosClient.putObject(putObjectRequest);
            //操作完成关闭cosClient实例
            cosClient.shutdown();
            jsonResult.setCode("1");
            jsonResult.set("imgName",id+"."+suffix);
            jsonResult.setMsg("上传成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("上传格式不正确");
        }
        return jsonResult.getMap();
    }

}
